/*
Date:20210509 11:38AM
key:类似运载货物那题，首先已知天数的范围，就在这里选择合适的天数去卡结果，如果结果满足就右区间缩小，不满足就左区间缩小。
    花的连续性在这里其实不重要的。因为每个num【i】都是独立检测是否满足lim，当然不满足的要重置tmp
*/
class Solution {
public:
    bool check(vector<int>& bloomDay,int&lim,int&k,int&m)
    {
        int re=0;
        int tmp=0;
        for(int i=0;i<bloomDay.size();i++)
        {
            if(bloomDay[i]<=lim)
            {tmp++;}
            else
            {
                tmp=0;
                continue;
            }
            if(tmp==k)
            {
                tmp=0;
                re++;
            }
        }
        //cout<<"re:"<<re<<endl;
        return re>=m;
    }
    int minDays(vector<int>& bloomDay, int m, int k) 
    {
        if(bloomDay.size()<m*k){return -1;}
        int l=*min_element(bloomDay.begin(),bloomDay.end());
        int r=*max_element(bloomDay.begin(),bloomDay.end());
        while(l<r)
        {
            int mid=(l+r)>>1;
            //cout<<mid<<endl;
            if(check(bloomDay,mid,k,m))
            {
                r=mid;
                
            }
            else
            {
                l=mid+1;
                //cout<<l<<endl;
            }
        }
        return r;
    }
};